﻿App.define('signalr', [], (function () {
	var signalr = {},
		hubs = ['defaultHub', 'gameHub'];
	try {
		signalr.ping = App.emptyFn;

		signalr.getHub = function (hub) {
			return $.connection[hub];
		};

		signalr.client = function (par) {
			signalr.getHub(par.hub).on(par.action, par.fn);
		};

		signalr.server = function (par) {
			var fn = signalr.getHub(par.hub).server[par.action];
			if (App.isUndefined(par.data)) {
				return fn();
			}
			return fn(par.data);
		};

		signalr.start = function () {
			return $.connection.hub.start();
		};

		signalr.stop = function () {
			$.connection.hub.stop();
		};

		$.each(hubs, function (i, item) {
			signalr.client({
				hub: item,
				action: 'Pong',
				fn: App.emptyFn
			});
		});

		signalr.ping = function (data) {
			signalr.server({
				hub: 'defaultHub',
				action: 'ping',
				data: data
			});
		};

		$.connection.hub.disconnected(function () {
			var i = 0, val;
			var interval = setInterval(function () {
				i += 5;
				val = 'grayscale({0}%)'.replace('{0}', i);

				$(document.body).css({
					'-webkit-filter': val,
					'-moz-filter' : val,
					filter : val
				});

				if (i == 100) {
					clearInterval(interval);
					$("#disconnectPopup").click();
				}
			}, 50);
			$(window).unbind('beforeunload');
		});

	} catch (e) {
		console.error('signalr.module.js', e);
	}

	return signalr;
})())